Technote 1173Understanding Open Transport Asset TrackingBy Quinn "The Eskimo!"Apple Worldwide Developer Technical Support |
CONTENTSGoals and ProblemsThe Original Solution The Carbon Solution Summary |
This Technote describes how Open Transport tracks assets, like memory and providers, which it allocates on your behalf. Open Transport’s approach to asset tracking was poorly documented in the past, and that contributed to a number of common developer programming errors. This technote describes the asset tracking mechanism used for the existing Open Transport programming interface, and how that mechanism has been “tuned up” for Carbon. This Technote is directed at all programmers who use the Open Transport client programming interface. |
Goals and ProblemsOpen Transport regularly allocates assets on your behalf. For example, every time you open an endpoint, Open Transport creates an internal data structure to hold the state of that endpoint. Whenever Open Transport allocates an asset, it associates the asset with the particular client that allocated it, so when the client terminates, Open Transport can clean up the assets it allocated.
Open Transport tracks two important classes of assets,
providers (endpoints, mappers, service providers) and memory
(allocated by Other parts of Mac OS implement various different asset tracking schemes:
Unfortunately, Open Transport cannot use any of the asset tracking schemes described above. The Open Transport programming interface is typically called at deferred task time, and tracking assets by the current process or heap zone does not make sense at deferred task time. For example, imagine that your application calls
Because of these problems, Open Transport 1.0 introduced a new asset tracking mechanism, which is described in the next section. However, this mechanism proved somewhat confusing to developers, so Carbon introduces a much clearer mechanism, based on explicit contexts. |
The Original SolutionThis section describes how Open Transport tracks assets for pre-Carbon clients, and the rules you should follow when building pre-Carbon software. The BasicsOpen Transport tracks assets for pre-Carbon clients by
means of a global variable, named
For example, when you call
The “Priv” routine is the real entry point into the
operating system, exported by the Open Transport shared
libraries. Open Transport uses the address of the client
record to uniquely identify the client creating the asset,
and tags the asset as belonging to that client. When the
client terminates (either it calls
The
Applications versus ExtensionsTo further complicate matters, Open Transport has two sets of static object files. The set whose name contains “App” is only suitable for applications, while the set whose name contains “Extn” is suitable for other, non-application code (system extensions, shared libraries, MPW tools, and so on). Your choice of libraries affects the following.
Rules for ClientsOpen Transport’s asset tracking strategy has a number of important consequences for developers.
Tracked AssetsThe follow table shows exactly which routines are have
“Priv” variants. Except as noted below, the assets created
by these routines are tracked by Open Transport and will be
disposed of when the client calls
Notes:
|
The Carbon SolutionThe original solution to the Open Transport asset tracking problem was creative, but it has a number of problems.
To address these problems—especially the problems with static object files—the designers of Carbon chose to restructure the Open Transport programming interface for Carbon programs. This minor update, which is in line with the general “tune up” goal of Carbon, is described in the next section. The BasicsThe Open Transport programming interface for Carbon
programs makes the client record an explicit parameter to
the routines which need it. For example, rather than call
Your program must create an Open Transport context
explicitly by calling
Carbon also includes the concept of a default application
context. If you pass Finally, to help application developers maintain source code compatibility between their Carbon and pre-Carbon source bases, the Carbon designers introduced a set of macros that map the pre-Carbon routines to their Carbon equivalents. For example, the following macro allows you to open an endpoint under Carbon without changing your pre-Carbon source.
To access these macros, you must define the
Tracked Assets In CarbonThe follow table shows exactly which routines are
“InContext” under Carbon. Except as noted below, the assets
created by these routines are tracked by Open Transport and
will be disposed of when the client calls
Notes:
|
SummaryUnderstanding how Open Transport tracks assets is
important when writing applications, shared libraries, code
resources, and other software that calls Open Transport
reliably. The most important rule for pre-Carbon clients is
that you must call |
|
Thanks to Joe Holt, Rich Kubota, and Vincent Lubet.